package com.marketingexpert.dao.ibatis;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.marketingexpert.dao.StreamDao;
import com.marketingexpert.domain.Keyword;
import com.marketingexpert.domain.Panel;
import com.marketingexpert.domain.Tab;
import com.marketingexpert.util.Resources;

public class SqlMapStreamDao extends SqlMapClientDaoSupport  implements StreamDao {

	private static Logger logger = Logger.getLogger(SqlMapStreamDao.class);
	
	public int saveTab(Tab tab)  throws DataAccessException{
		
		try {
		int tabId = (Integer)getSqlMapClientTemplate().insert("insertTab",tab);
		return tabId;
		} catch(DataAccessException e) {
			e.printStackTrace();
			throw e;
		}
	}

	public int queryTabCount(String userId) throws DataAccessException {
		try {
			List<Tab> tabs = getSqlMapClientTemplate().queryForList("selectTabByUserId",userId);
			int tabCount = tabs.size();
			return tabCount;
			} catch(DataAccessException e) {
				e.printStackTrace();
				throw e;
			}
	}

	public int savePanel(Panel panel) throws DataAccessException {
		try {
			int tabId = (Integer)getSqlMapClientTemplate().insert("insertPanel",panel);
			return tabId;
			} catch(DataAccessException e) {
				e.printStackTrace();
				throw e;
			}
	}

	public Panel queryPanelByTabId(int tabId) throws DataAccessException {
		try {
			Panel panel = (Panel) getSqlMapClientTemplate().queryForObject("getPanelByTabId",tabId);
			return panel;
			} catch(DataAccessException e) {
				e.printStackTrace();
				throw e;
			}
	}

	public Keyword queryKeywordsByPanelId(int panelId)
			throws DataAccessException {
		try {
			Keyword keywords = (Keyword) getSqlMapClientTemplate().queryForObject("selectKeywordsByPanelId",panelId);
			return keywords;
			} catch(DataAccessException e) {
				e.printStackTrace();
				throw e;
			}
	}

	public List<Keyword> insertKeywords(List<Keyword> keywords, int panelId)
			throws DataAccessException {
		
		List<Keyword> returnKeywords = new ArrayList<Keyword>();
		
		try {
			for(Keyword keyword:keywords) {
				int kId = (Integer)getSqlMapClientTemplate().insert("insertKeywords", keyword);
				Keyword key =  (Keyword)getSqlMapClientTemplate().queryForObject("selectKeywordsById", kId);
				returnKeywords.add(key);
			}
			return returnKeywords;
		} catch(DataAccessException e) {
			logger.error(Resources.getString("Stream.queryKeywordsByPanelIdFailed",new String[]{"SqlMapStreamDao,Line 89!"}), e);
			throw e;
		}
		
	}

	public List<Tab> insertKeywords(String parentUserId)
			throws DataAccessException {
		try {
			List<Tab> tabs = getSqlMapClientTemplate().queryForList("selectTabByUserId",parentUserId);
			return tabs;
			} catch(DataAccessException e) {
				e.printStackTrace();
				throw e;
			}
	}

	public int queryPanelCountsByTabId(int tabId) throws DataAccessException {
		try {
			int panelCounts = (Integer)getSqlMapClientTemplate().queryForObject("queryPanelCountsByTabId",tabId);
			return panelCounts;
			} catch(DataAccessException e) {
				e.printStackTrace();
				throw e;
			}
	}

	public List<Panel> savePanelBatch(List<Panel> panels)
			throws DataAccessException {
		try {
			List<Panel> returnPanel  = new ArrayList<Panel>();
			for(Panel panel: panels) {
				int panelId = (Integer)getSqlMapClientTemplate().insert("insertPanel",panel);
				panel.setId(panelId);
				Keyword keyword = panel.getKeyword();
				keyword.setPanelId(panelId);
				int kId = (Integer)getSqlMapClientTemplate().insert("insertKeywords", keyword);
				keyword.setId(kId);
				panel.setKeyword(keyword);
				returnPanel.add(panel);
			}
			return returnPanel;
			} catch(DataAccessException e) {
				e.printStackTrace();
				throw e;
			}
	}

	public Tab queryTabById(int tabId) throws DataAccessException {
		try {
			Tab tab = (Tab) getSqlMapClientTemplate().queryForObject("queryTabById",tabId);
			return tab;
			} catch(DataAccessException e) {
				e.printStackTrace();
				throw e;
			}
	}

	public void deleteTabByUserId(String userId) throws DataAccessException {
		try {
			getSqlMapClientTemplate().delete("deleteTabByUserId",userId);
			} catch(DataAccessException e) {
				e.printStackTrace();
				throw e;
			}
		
	}

	public void deletePanle(int panelId)
			throws DataAccessException {
		try {
			getSqlMapClientTemplate().delete("deletePanle",panelId);
			} catch(DataAccessException e) {
				e.printStackTrace();
				throw e;
			}
		
	}

	public void deleteTabById(int tabId) throws DataAccessException {
		try {
			getSqlMapClientTemplate().delete("deleteTabById",tabId);
			} catch(DataAccessException e) {
				e.printStackTrace();
				throw e;
			}
	}

}
